package cn.iocoder.yudao.module.bpm.service.formdefinition;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.bpm.controller.admin.formdefinition.vo.*;
import cn.iocoder.yudao.module.bpm.dal.dataobject.formdefinition.FormDefinitionDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.bpm.convert.formdefinition.FormDefinitionConvert;
import cn.iocoder.yudao.module.bpm.dal.mysql.formdefinition.FormDefinitionMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.bpm.enums.ErrorCodeConstants.*;

/**
 * 表单定义 Service 实现类
 *
 * @author admin
 */
@Service
@Validated
public class FormDefinitionServiceImpl implements FormDefinitionService {

    @Resource
    private FormDefinitionMapper formDefinitionMapper;

    @Override
    public Long createFormDefinition(FormDefinitionCreateReqVO createReqVO) {
        // 插入
        FormDefinitionDO formDefinition = FormDefinitionConvert.INSTANCE.convert(createReqVO);
        formDefinitionMapper.insert(formDefinition);
        // 返回
        return formDefinition.getId();
    }

    @Override
    public void updateFormDefinition(FormDefinitionUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateFormDefinitionExists(updateReqVO.getId());
        // 更新
        FormDefinitionDO updateObj = FormDefinitionConvert.INSTANCE.convert(updateReqVO);
        formDefinitionMapper.updateById(updateObj);
    }

    @Override
    public void deleteFormDefinition(Long id) {
        // 校验存在
        this.validateFormDefinitionExists(id);
        // 删除
        formDefinitionMapper.deleteById(id);
    }

    private void validateFormDefinitionExists(Long id) {
        if (formDefinitionMapper.selectById(id) == null) {
            throw exception(FORM_DEFINITION_NOT_EXISTS);
        }
    }

    @Override
    public FormDefinitionDO getFormDefinition(Long id) {
        return formDefinitionMapper.selectById(id);
    }

    @Override
    public List<FormDefinitionDO> getFormDefinitionList(Collection<Long> ids) {
        return formDefinitionMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<FormDefinitionDO> getFormDefinitionPage(FormDefinitionPageReqVO pageReqVO) {
        return formDefinitionMapper.selectPage(pageReqVO);
    }

    @Override
    public List<FormDefinitionDO> getFormDefinitionList(FormDefinitionExportReqVO exportReqVO) {
        return formDefinitionMapper.selectList(exportReqVO);
    }

}
